package com.song.estore.action;

import com.song.estore.dao.UserDao;
import com.song.estore.domain.User;
import com.song.estore.service.UserService;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.Cookie;

/**
 * Created by 范姣姣 on 2015/10/22.
 *
 */
public class LoginAction {

    private String email;
    private String password;
    private String remember;
    private String msg;

    private UserService userService;

    public String execute(){

        if( email == null || password == null ||
                email.trim().length() == 0 || password.trim().length() == 0){
            msg = "邮箱不能为空";
            return "error";
        }


        User user = userService.findUserLogin(email, password);

        if( user == null ){
            msg = "账号密码错误";
            return "error";
        }

        if( remember != null && remember.equals("on") ){
            Cookie cookie = new Cookie("username",user.getEmail());
            cookie.setPath("/");
            cookie.setMaxAge(60*60*24*7);
            ServletActionContext.getResponse().addCookie(cookie);
        }
        ServletActionContext.getRequest().getSession().setAttribute("loginUser", user);
        return "success";
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getRemember() {
        return remember;
    }

    public void setRemember(String remember) {
        this.remember = remember;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
